package com.lonwin.inspectionmanagement.mvp.presenter;

import android.app.Application;
import android.util.ArrayMap;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.EditPatrolRecordContract;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.XcjlbInfoBean;

import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/24/2018 23:31
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class EditPatrolRecordPresenter extends BasePresenter<EditPatrolRecordContract.Model, EditPatrolRecordContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    ConfigBean configBean;
    @Inject
    public EditPatrolRecordPresenter(EditPatrolRecordContract.Model model, EditPatrolRecordContract.View rootView) {
        super(model, rootView);
        configBean=DataHelper.getDeviceData(mRootView.getMyActivity(),AppConstants.EXTRA_CONFIG_DATA);

    }

    public ConfigBean getConfigBean() {
        return configBean;
    }



    /**
     * 编辑巡查记录表
     * @param params
     */
    public void  editXcjlb( Map<String, String> params) {
            mModel.editXcjlb(DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/editXcjlb",params).compose(RxUtils.applySchedulers(mRootView))
                    .subscribe(new ErrorHandleSubscriber<XcjlbInfoBean>(mErrorHandler) {
                        @Override
                        public void onNext(XcjlbInfoBean xcjlbInfoBean) {
                            if (xcjlbInfoBean!=null&&xcjlbInfoBean.getCode()==0)
                            {
                                mRootView.showMessage("编辑成功");
                                mRootView.editXcjlbSuccess(xcjlbInfoBean.getData());
                            }else {
                                 mRootView.showMessage("编辑失败");
                            }

                        }
                    });
        }
        /**
         * 获取巡查记录表详情
         * @param xcjlbid
         */
    public void getXcjlbById(String xcjlbid)
    {
        Map<String,Object> maps=new ArrayMap<>();
        maps.put("xcjlbid",xcjlbid);
        mModel.getXcjlbById(DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/getXcjlbById",maps)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<XcjlbInfoBean>(mErrorHandler) {
                    @Override
                    public void onNext(XcjlbInfoBean xcjlbInfoBean) {
                        mRootView.getXcjlbByIdSuccess(xcjlbInfoBean.getData());
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
